package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.product.domain.Category;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.vo.CategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-02-19
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    /**
     * @param id 0：查询所有顶级分类 如果其他值根据值查询子分类
     * @return 集合中分类对象 通过“hasChildren”属性判断是否有子分类
     */
    @Override
    public List<Category> treeSelect(Long id) {
        //1.根据id查询包含子分类
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId, id);
        List<Category> categoryList = baseMapper.selectList(queryWrapper);
        //2.遍历查询到分类列表，判断是否包含子分类
        if (!CollectionUtils.isEmpty(categoryList)) {
            for (Category category : categoryList) {
                //根据当前分类ID查询子分类 数量大于>0说明有子分类
                Long count = baseMapper.selectCount(new LambdaQueryWrapper<Category>().eq(Category::getParentId, category.getId()));
                category.setHasChildren(count > 0);
            }
        }
        return categoryList;
    }

    /**
     * 查询一级分类列表
     *
     * @return
     */
    @Override
    public List<CategoryVo> selectTopCategoryList() {
        //1.查询所有一级分类
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId, 0);
        List<Category> categoryList = baseMapper.selectList(queryWrapper);

        //2.封装成CategoryVo 采用Stream流处理集合
        if (!CollectionUtils.isEmpty(categoryList)) {
            List<CategoryVo> categoryVoList = categoryList
                    .stream()
                    .map(category -> {
                        CategoryVo categoryVo = new CategoryVo();
                        BeanUtils.copyProperties(category, categoryVo);
                        return categoryVo;
                    }).collect(Collectors.toList());
            return categoryVoList;
        }
        return null;
    }

    /**
     * 查询所有分类，将子分类封装到children属性中
     *
     * @return
     */
    @Override
    public List<CategoryVo> selectAllCategoryList() {
        //1.查询所有分类 共计：703条记录
        List<Category> allCategoryList = baseMapper.selectList(null);

        //2.将所有分类封装成CategoryVo
        if (!CollectionUtils.isEmpty(allCategoryList)) {
            List<CategoryVo> categoryVoList = allCategoryList.stream().map(category -> {
                CategoryVo categoryVo = new CategoryVo();
                BeanUtils.copyProperties(category, categoryVo);
                return categoryVo;
            }).collect(Collectors.toList());
            //3.调用内部方法处理树结构
            return buildTree(categoryVoList);
        }
        return null;
    }

    /**
     * 递归构建树形结构
     *
     * @param categoryVoList
     * @return
     */
    private List<CategoryVo> buildTree(List<CategoryVo> categoryVoList) {
        //0.定义Map存放所有分类数据 key:分类ID  Value：分类对象
        Map<Long, CategoryVo> categoryVoHashMap = new HashMap<>();
        for (CategoryVo categoryVo : categoryVoList) {
            categoryVoHashMap.put(categoryVo.getId(), categoryVo);
        }
        List<CategoryVo> voList = new ArrayList<>();

        //1.遍历所有分类，通过判断parent_id=0 找出顶级分类
        for (CategoryVo categoryVo : categoryVoList) {
            if (categoryVo.getParentId().intValue() == 0) {
                voList.add(categoryVo);
            } else {
                //2.如果是子节点，找到他的父节点，并添加到父节点的children属性中
                Long currentParentId = categoryVo.getParentId();
                //3.根据父节点ID获取父节点
                CategoryVo parentCategory = categoryVoHashMap.get(currentParentId);
                parentCategory.getChildren().add(categoryVo);
            }
        }
        return voList;
    }
}
